using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.WebTesting;
using WebAndLoadTestPlugins;

namespace PluginTests
{
    /// <summary>
    /// Summary description for ExtractInputFieldTest
    /// </summary>
    [TestClass]
    public class ExtractTextTest
    {
        public ExtractTextTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            PageManager.Initialize(testContext);
        }

        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        public void TestExtractIndex(string startsWith, string endsWith, int index, ExtractText.SearchResult expectedSearchResult, string expectedString)
        {
            ExtractText extractText = new ExtractText();
            extractText.StartsWith = startsWith;
            extractText.EndsWith = endsWith;
            extractText.Index = index;

            string doc;
            PageManager.Instance.LoadPage("ExtractTextTest.htm", out doc);

            ExtractText.SearchResult result = extractText.Extract(doc);
            Assert.AreEqual(result, expectedSearchResult);
            if (expectedSearchResult == ExtractText.SearchResult.Found)
            {
                Assert.AreEqual(extractText.Result, expectedString);
            }
        }

        [TestMethod]
        public void TestExtractIndex()
        {
            TestExtractIndex("?param1=", "&", 0, ExtractText.SearchResult.Found, "i1value1");
            TestExtractIndex("?param1=", "&", 0, ExtractText.SearchResult.Found, "i1value1");
            TestExtractIndex("?param1=", "&", 6, ExtractText.SearchResult.EndsWithNotFound, "");
            TestExtractIndex("?param1=p", "&", 6, ExtractText.SearchResult.StartsWithNotFound, "");
        }

        [TestMethod]
        public void TestExtractRandom()
        {
            string doc;
            PageManager.Instance.LoadPage("ExtractTextTest.htm", out doc);

            List<string> extractedTextList = new List<string>();
            for (int i = 0; i < 5; i++)
            {
                ExtractText extractText = new ExtractText();
                extractText.StartsWith = "?param1=";
                extractText.EndsWith = "&";
                extractText.ExtractRandomMatch = true;
                System.Threading.Thread.Sleep(50);
                ExtractText.SearchResult result = extractText.Extract(doc);
                extractedTextList.Add(extractText.Result);
            }

            // validate not all strings are the same
            string first = extractedTextList[0];
            bool equal = true;
            for (int i=1; i<5; i++)
            {
                equal = first == extractedTextList[i];
                if (!equal)
                    break;
            }
            Assert.IsFalse(equal);
        }

    }
}
